<template>
  <div>
    <el-dialog title="添加在线商品到分组" :visible.sync="open" width="80%" :before-close="close">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="是否已在分组" prop="groupStatus">
          <el-select size="small" v-model="queryParams.groupStatus" placeholder="请选择是否已绑定套图方案"
                     clearable filterable>
            <el-option v-for="(item,index) in isBingList" :key="index" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品标题" prop="subject">
          <el-input v-model="queryParams.subject" placeholder="请输入商品标题" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="速卖通产品Id" prop="aeProductId">
          <el-input v-model="queryParams.aeProductId" @keyup.enter.native="handleQuery"
                    placeholder="请输入速卖通产品Id搜索" clearable></el-input>
        </el-form-item>
        <el-form-item label="产品Id" prop="id">
          <el-input v-model="queryParams.id" @keyup.enter.native="handleQuery"
                    placeholder="请输入产品Id搜索" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="collectProductList" @selection-change="handleSelectionChange" stripe border>
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="产品Id" align="center" prop="id"/>
        <el-table-column label="速卖通产品ID" align="center" prop="aeProductId"/>
        <el-table-column show-overflow-tooltip label="商品标题" align="center" prop="subject"/>
        <el-table-column label="商品图" align="center" prop="white" width="100">
          <template slot-scope="scope">
            <imagePreview :src="scope.row.white" width="60px" height="60px"/>
          </template>
        </el-table-column>
        <el-table-column label="价格" align="center" prop="productPrice"/>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :pageSizes="[5, 10, 20]"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {bindingProdudtForGroup} from "@/api/seller/shopDecoration"
import {listSellerStoreProduct,} from "@/api/product/online/list"
import {sellerStoreProductMergingProgram} from "@/api/product/online/productSet";

export default {
  name: "GoodsList",
  dicts: ['group_task_status'],
  props: {
    groupId: {
      type: String,
      default: ''
    },
    storeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采集商品表格数据
      collectProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        id: undefined,
        subject: undefined,
        collectUrl: undefined,
        skuCode: undefined,
        storeId: this.storeId,
        groupStatus: '0',//是否在分组中
        groupTaskStatus: undefined,
        productStatusType: '1',//ERP在售商品
        groupId: this.groupId
      },
      isBingList: [{
        label: '是',
        value: '1'
      }, {
        label: '否',
        value: '0'
      }],
    };
  },
  created() {
    this.getList()
  },
  methods: {
    // 使用方案
    useProgramme(row) {
      this.$confirm('是否确定使用此方案？').then(() => {
        sellerStoreProductMergingProgram({taskId: row.id}).then(res => {
          this.$message.success(res.msg)
        })
      })
    },
    // 关闭弹窗
    close(e) {
      this.$emit('close', e)
    },
    // 添加商品到分组
    handleAdd() {
      this.$modal.confirm('是否将选中的商品添加到分组中？').then(() => {
        const params = {
          productId: this.ids.toString(),
          groupId: this.groupId
        }
        bindingProdudtForGroup(params).then(res => {
          this.getList();
          this.$modal.msgSuccess(res.msg);
        })
      })
    },
    // 获取店铺下在线商品列表
    getList() {
      this.loading = true;
      listSellerStoreProduct(this.queryParams).then(res => {
        this.collectProductList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    /** 转换采集商品类目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.mergingProgramId = undefined
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
};
</script>

<style lang="scss">

</style>

